package com.geovis.emergency.spd.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.system.service.ISystemLogService;
import com.geovis.emergency.spd.cache.UserCache;
import com.geovis.emergency.spd.entity.system.entity.SystemLog;
import com.geovis.emergency.spd.entity.system.entity.SystemUser;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemLogQueryDTO;
import com.geovis.emergency.spd.entity.system.pojo.vo.SystemLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  系统管理模块-系统日志相关接口
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/8/3
 */
@RestController
@RequestMapping("/system/systemLog")
@AllArgsConstructor
@Api(value = "系统管理模块-系统日志相关接口", tags = "系统管理模块-系统日志相关接口")
@Slf4j
public class SystemLogController extends BaseController<ISystemLogService> {

    @ApiOperation(value = "分页查询日志列表", notes = "分页查询日志列表")
    @PostMapping(value = "/listPage")
    public Result<PageResult<SystemLogVO>> listPage(@RequestBody @Validated PageParam<SystemLogQueryDTO> pageParam) {
        PageResult<SystemLog> systemLogPageResult = baseService.listPage(pageParam);
        List<SystemLogVO> records = systemLogPageResult.getRecords().stream().map(systemLog -> {
            SystemLogVO systemLogVo = BeanUtil.toBean(systemLog, SystemLogVO.class);
            if (ObjectUtil.isNotEmpty(systemLogVo.getCreateBy())){
                systemLogVo.setCreateUser(systemLogVo.getCreateBy());
            }
            return systemLogVo;
        }).collect(Collectors.toList());
        IPage<SystemLogVO> page = new Page<>();
        page.setTotal(systemLogPageResult.getTotal());
        page.setRecords(records);
        PageResult<SystemLogVO> pageResult = new PageResult<>(page);
        return Result.ok(pageResult);
    }

}
